<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <meta charset="UTF-8">
    <title>用户管理</title>
    {% include 'system/common/header.html' %}
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="user-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">用户</label>
                <div class="layui-input-inline">
                    <input type="text" name="realname" placeholder="" class="layui-input">
                </div>
                <label class="layui-form-label">账号</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="user-table" lay-filter="user-table"></table>
        </div>
    </div>
</div>
{# 表格操作 #}
<script type="text/html" id="user-toolbar">
    {% if authorize("users:admin:add") %}
        <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
            <i class="pear-icon pear-icon-add"></i>
            新增
        </button>
    {% endif %}
    <button class="pear-btn pear-btn-md" lay-event="collasped">
        <i class="pear-icon pear-icon-modular"></i>
        高级
    </button>
</script>

{# 用户修改操作 #}
<script type="text/html" id="user-bar">
    {% if authorize("users:admin:edit") %}
        <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="pear-icon pear-icon-edit"> 编辑</i>
        </button>
    {% endif %}
    {% if authorize("users:admin:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="pear-icon pear-icon-ashbin"> 删除</i>
        </button>
    {% endif %}
</script>

{% include 'system/common/footer.html' %}
<script>
    layui.use(['table', 'form', 'jquery', 'common','popup'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let popup = layui.popup;
        let MODULE_PATH = "/users/admin/";
        // 数据表格
        let cols = [
            [
                {title: 'ID', field: 'id', align: 'center'},
                {title: '账号', field: 'username', align: 'center'},
                {title: '用户', field: 'truename', align: 'center'},
                {title: '手机', field: 'mobile', align: 'center'},
                {title: '操作', toolbar: '#user-bar', align: 'center', width: 200}
            ]
        ]
        window.render=function(){
            // 渲染表格
            table.render({
                elem: '#user-table',
                url: MODULE_PATH + 'data',
                page: true,
                cols: cols,
                skin: 'line',
                toolbar: '#user-toolbar',
                defaultToolbar: [{
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh'},
                    'filter', 'print', 'export']
            })
        }


        table.on('toolbar(user-table)', function (obj) {
            if (obj.event === 'add') {
                window.add()
            }
        })

        table.on('tool(user-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj)
            }else if (obj.event === 'edit') {
                window.edit(obj)
            }
        })

        window.add = function () {
            layer.open({
                type: 2,
                title: '新增用户',
                shade: 0.1,
                scrollbar: false,
                area: ['550px', '550px'],
                content: MODULE_PATH + '/add'
            })
        }
        window.edit = function (obj) {
            layer.open({
                type: 2,
                title: '编辑用户',
                shade: 0.1,
                scrollbar: false,
                area: ['550px', '550px'],
                content: MODULE_PATH + '/edit?id=' + obj.data.id
            })
        }
        window.remove = function (obj) {
            layer.confirm('确定要删除该用户吗？', {icon: 3, title: '提示'}, function (index) {
                $.ajax({
                    url: MODULE_PATH + '/remove',
                    type: 'POST',
                    data: {id: obj.data.id},
                    success: function (res) {
                        if (res.code === 0) {
                            layer.msg(res.msg)
                            table.reload()
                        }else {
                            layer.msg(res.msg, {icon: 2})
                        }
                    }
                })
            })
        }
    window.render()
    })
</script>
</body>
</html>